package com.laikan.reader.core.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import com.laikan.reader.core.service.UpdateService;
import com.laikan.reader.R;

/**
 * 检测版本更新工具类
 *
 */
public class VersionUpdateUtil {

    private static Context mContext;
    private static Activity act;
    private static String versionname = null;// 本地安装包版本信息
    private static String newversionName = null;// 从网络获得的版本信息
    private static boolean isNeedUpdate = false;// 是否更新

    public VersionUpdateUtil(Activity act, Context context) {
        VersionUpdateUtil.act = act;
        mContext = context;
        getCurrentVersion();
        check();
    }

    /**
     * 检查是否有新版本
     *
     * @param newVersionName 服务器返回的版本
     * @param versionName 当前版本
     *
     * @return 服务器获取到的版本号是否比当前版本号高
     */
    private static boolean needUpdate(String newVersionName, String versionName) {
        if (null != newVersionName && !"".equals(newVersionName)) {
            String[] newValues = newVersionName.split("\\.");
            String[] values = versionName.split("\\.");
            int len = 0;
            // 长度一样s
            if (newValues.length == values.length) {
                // new 1.4 / 1.2
                // v 1.3 / 1.4
                len = newValues.length;
                for (int i = 0; i < len; i++) {
                    int newCode = Integer.parseInt(newValues[i]);
                    int code = Integer.parseInt(values[i]);
                    if (newCode > code) {
                        return true;
                    }
                }
            }else if (newValues.length > values.length) {
                // new 1.3.3 1.2.5
                // v 1.3 / 1.4
                len = values.length;
                for (int i = 0; i < len; i++) {
                    int newCode = Integer.parseInt(newValues[i]);
                    int code = Integer.parseInt(values[i]);
                    if (newCode > code) {
                        return true;
                    }else if (i == (len - 1)) {
                        if (newCode == code) {
                            return true;
                        }
                    }
                }
            }else if (newValues.length < values.length) {
                // new 1.2 - 1.4 - 1.3
                // v 1.3.3 - 1.2.4
                len = newValues.length;
                for (int i = 0; i < len; i++) {
                    int newCode = Integer.parseInt(newValues[i]);
                    int code = Integer.parseInt(values[i]);
                    if (newCode > code) {
                        return true;
                    }
                }
            }
        }

        return false;

    }

    public void getCurrentVersion() {
        PackageManager pm = mContext.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0);
            versionname = pi.versionName;
        }catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        // TODO 从服务器获取最新的版本信息(需要自己写)
    }

    public void check() {

        isNeedUpdate = needUpdate(newversionName, versionname);

        if (isNeedUpdate) {// Check version.
            showUpdateDialog();
        }
    }

    public void showUpdateDialog() {

        AlertDialog alert = new AlertDialog.Builder(mContext).setTitle("版本更新").setIcon(mContext.getResources().getDrawable(R.mipmap.ic_launcher)).setMessage("检测到新版本，是否更新？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setClass(mContext, UpdateService.class);
                intent.putExtra("APP_NAME", mContext.getResources().getString(R.string.fw_app_name));
                mContext.startService(intent);
            }
        }).setNegativeButton("稍后再说", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        }).show();
    }
}
